package com.irs.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.irs.mapper.TbArticleMapper;
import com.irs.mapper.TbCategoryMapper;
import com.irs.pojo.TbCategory;
import com.irs.pojo.TbCategoryExample;
import com.irs.pojo.TbCategoryExample.Criteria;
import com.irs.service.CategoryService;
import com.irs.util.ResultUtil;
@Service
public class CategoryServiceImpl implements CategoryService{
	@Autowired
	private TbCategoryMapper TbCategoryMapper;
	@Override
	public ResultUtil selCategorys(Integer page, Integer limit) {
		PageHelper.startPage(page, limit);
		TbCategoryExample example = new TbCategoryExample();
		example.setOrderByClause("id DESC");
		List<TbCategory> list = TbCategoryMapper.selectByExample(example);
		PageInfo<TbCategory> pageInfo = new PageInfo<TbCategory>(list);
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}
	@Override
	public void insCategoryService(TbCategory category){
		// TODO Auto-generated method stub
		TbCategoryMapper.insert(category);
		
	}
	@Override
	public TbCategory selCategoryByname(String category) {
		// TODO Auto-generated method stub
		TbCategoryExample example = new TbCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryEqualTo(category);
		List<TbCategory> categorys =TbCategoryMapper.selectByExample(example);
		if(categorys!=null&&categorys.size()>0){
			return categorys.get(0);
		}
		return null;
	}
	@Override
	public void delCategoryById(String id) {
		// TODO Auto-generated method stub
		TbCategoryMapper.deleteCategoryById(Long.parseLong(id));
	}
	@Override
	public void delCategorysService(String categoryStr) {
		// TODO Auto-generated method stub
		String[] category = categoryStr.split(",");
		if(category!=null && category.length>0){
			for(String id:category){
				TbCategoryMapper.deleteCategoryById(Long.parseLong(id));
			}
		}
		
	}
	@Override
	public void updateCategoryById(TbCategory category) {
		// TODO Auto-generated method stub
		TbCategory c =  TbCategoryMapper.selectById(category.getId());
		c.setCategory(category.getCategory());
		TbCategoryMapper.updateById(c);
		
	}
	@Override
	public TbCategory selCategoryById(String id) {
		// TODO Auto-generated method stub
		
		return TbCategoryMapper.selectById(Long.parseLong(id));
	}
	@Override
	public List<TbCategory> selectAll() {
		// TODO Auto-generated method stub
		return TbCategoryMapper.selectAll();
	}
	

}
